{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T11:40:41.224102Z",
     "start_time": "2022-01-29T11:40:40.538102Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T11:41:31.137702Z",
     "start_time": "2022-01-29T11:41:31.132702Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('电压原始数据.txt') as f:\n",
    "    content = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T11:45:12.172502Z",
     "start_time": "2022-01-29T11:45:12.164502Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'No. 1 : BOARD DIAGNOSE:SUBNET=530301,NE=729789,RACK=1,SHELF=1,SLOT=14,FUNCTION_ID=16777224;\\n------------- QJ_OMMB1_6.48.254.180 -------------\\n记录号 |诊断类型号 |诊断类型      |测试项号 |测试项        |位置      |结果   \\n------------------------------------------------------------------------------\\n1      16777224    PM单板诊断测试 100400    模块温度       PM(1.1.14) 31.0°C \\n2      16777224    PM单板诊断测试 100401    输入电源电压   PM(1.1.14) 53.0V   \\n3      16777224    PM单板诊断测试 100402    管理电源电压   PM(1.1.14) 3.4V    \\n4      16777224    PM单板诊断测试 100403    业务电源电压   PM(1.1.14) 11.7V   \\n5      16777224    PM单板诊断测试 100425    PM板的工作模式 PM(1.1.14) 主备    \\n------------------------------------------------------------------------------\\n记录数:'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'No. 2 : BOARD DIAGNOSE:SUBNET=530301,NE=730973,RACK=1,SHELF=1,SLOT=14,FUNCTION_ID=16777224;\\n------------- QJ_OMMB1_6.48.254.180 -------------\\n记录号 |诊断类型号 |诊断类型      |测试项号 |测试项        |位置      |结果   \\n------------------------------------------------------------------------------\\n1      16777224    PM单板诊断测试 100400    模块温度       PM(1.1.14) 32.0°C \\n2      16777224    PM单板诊断测试 100401    输入电源电压   PM(1.1.14) 52.8V   \\n3      16777224    PM单板诊断测试 100402    管理电源电压   PM(1.1.14) 3.4V    \\n4      16777224    PM单板诊断测试 100403    业务电源电压   PM(1.1.14) 12.0V   \\n5      16777224    PM单板诊断测试 100425    PM板的工作模式 PM(1.1.14) 主备    \\n------------------------------------------------------------------------------\\n记录数:'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过正则表达式将文本分块\n",
    "p1 = r'No[\\s\\S]+?记录数:'\n",
    "res = re.findall(p1, content)\n",
    "res = list(res)\n",
    "res[0]\n",
    "res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T12:07:58.002304Z",
     "start_time": "2022-01-29T12:07:57.990304Z"
    }
   },
   "outputs": [],
   "source": [
    "subnet = []\n",
    "ne = []\n",
    "slot = []\n",
    "temprature = []\n",
    "voltage1 = []\n",
    "voltage2 = []\n",
    "voltage3 = []\n",
    "mode = []\n",
    "# 分别写每个字段的正则表达式\n",
    "p2 = r'SUBNET=(\\d+)'\n",
    "p3 = r'NE=(\\d+)'\n",
    "p4 = r'SLOT=(\\d+)'\n",
    "p5 = r'\\d+\\.\\d°C'\n",
    "p6 = r'输入电源电压[\\s\\S]+?(\\d+\\.\\dV)'\n",
    "p7 = r'管理电源电压[\\s\\S]+?(\\d+\\.\\dV)'\n",
    "p8 = r'业务电源电压[\\s\\S]+?(\\d+\\.\\dV)'\n",
    "p9 = r'PM板的工作模式[\\s\\S]+?([\\u4e00-\\u9fa5]{2,4})'\n",
    "\n",
    "# 循环分块之后的文本，用正则表达式匹配需要的字段\n",
    "for text in res:\n",
    "    subnet_tmp = re.search(p2,text)\n",
    "    if subnet_tmp:\n",
    "        subnet.append(subnet_tmp.group(1))\n",
    "    else:\n",
    "        subnet.append('-')\n",
    "    \n",
    "    ne_tmp = re.search(p3,text)\n",
    "    if ne_tmp:\n",
    "        ne.append(ne_tmp.group(1))\n",
    "    else:\n",
    "        ne.append('-')\n",
    "        \n",
    "    slot_tmp = re.search(p4,text)\n",
    "    if slot_tmp:\n",
    "        slot.append(slot_tmp.group(1))\n",
    "    else:\n",
    "        slot.append('-')\n",
    "        \n",
    "    temprature_tmp = re.search(p5,text)\n",
    "    if temprature_tmp:\n",
    "        temprature.append(temprature_tmp.group(0))\n",
    "    else:\n",
    "        temprature.append('-')\n",
    "        \n",
    "    \n",
    "    voltage1_tmp = re.search(p6,text)\n",
    "    if voltage1_tmp:\n",
    "        voltage1.append(voltage1_tmp.group(1))\n",
    "    else:\n",
    "        voltage1.append('-')\n",
    "        \n",
    "    \n",
    "    voltage2_tmp = re.search(p7,text)\n",
    "    if voltage2_tmp:\n",
    "        voltage2.append(voltage2_tmp.group(1))\n",
    "    else:\n",
    "        voltage2.append('-')\n",
    "        \n",
    "    \n",
    "    voltage3_tmp = re.search(p8,text)\n",
    "    if voltage3_tmp:\n",
    "        voltage3.append(voltage3_tmp.group(1))\n",
    "    else:\n",
    "        voltage3.append('-')\n",
    "        \n",
    "    \n",
    "    mode_tmp = re.search(p9,text)\n",
    "    if mode_tmp:\n",
    "        mode.append(mode_tmp.group(1))\n",
    "    else:\n",
    "        mode.append('-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T12:11:41.232104Z",
     "start_time": "2022-01-29T12:11:41.209104Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>子网_SUBNET</th>\n",
       "      <th>基站代码_NE</th>\n",
       "      <th>槽位号_SLOT</th>\n",
       "      <th>模块温度</th>\n",
       "      <th>输入电源电压</th>\n",
       "      <th>管理电源电压(V)</th>\n",
       "      <th>业务电源电压(V)</th>\n",
       "      <th>PM板的工作模式</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>530301</td>\n",
       "      <td>729789</td>\n",
       "      <td>14</td>\n",
       "      <td>31.0°C</td>\n",
       "      <td>53.0V</td>\n",
       "      <td>3.4V</td>\n",
       "      <td>11.7V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>530301</td>\n",
       "      <td>730973</td>\n",
       "      <td>14</td>\n",
       "      <td>32.0°C</td>\n",
       "      <td>52.8V</td>\n",
       "      <td>3.4V</td>\n",
       "      <td>12.0V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>530301</td>\n",
       "      <td>730339</td>\n",
       "      <td>14</td>\n",
       "      <td>34.0°C</td>\n",
       "      <td>52.2V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>530301</td>\n",
       "      <td>730951</td>\n",
       "      <td>14</td>\n",
       "      <td>27.0°C</td>\n",
       "      <td>53.0V</td>\n",
       "      <td>3.4V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>530301</td>\n",
       "      <td>730724</td>\n",
       "      <td>14</td>\n",
       "      <td>43.0°C</td>\n",
       "      <td>53.0V</td>\n",
       "      <td>3.4V</td>\n",
       "      <td>11.8V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>530301</td>\n",
       "      <td>730151</td>\n",
       "      <td>14</td>\n",
       "      <td>36.0°C</td>\n",
       "      <td>53.1V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>874001</td>\n",
       "      <td>588309</td>\n",
       "      <td>14</td>\n",
       "      <td>35.0°C</td>\n",
       "      <td>52.8V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>530301</td>\n",
       "      <td>730089</td>\n",
       "      <td>14</td>\n",
       "      <td>40.0°C</td>\n",
       "      <td>53.0V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>530301</td>\n",
       "      <td>730479</td>\n",
       "      <td>14</td>\n",
       "      <td>28.0°C</td>\n",
       "      <td>53.1V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>530301</td>\n",
       "      <td>729985</td>\n",
       "      <td>14</td>\n",
       "      <td>37.0°C</td>\n",
       "      <td>53.7V</td>\n",
       "      <td>3.5V</td>\n",
       "      <td>11.9V</td>\n",
       "      <td>主备</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  子网_SUBNET 基站代码_NE 槽位号_SLOT    模块温度 输入电源电压 管理电源电压(V) 业务电源电压(V) PM板的工作模式\n",
       "0    530301  729789       14  31.0°C  53.0V      3.4V     11.7V       主备\n",
       "1    530301  730973       14  32.0°C  52.8V      3.4V     12.0V       主备\n",
       "2    530301  730339       14  34.0°C  52.2V      3.5V     11.9V       主备\n",
       "3    530301  730951       14  27.0°C  53.0V      3.4V     11.9V       主备\n",
       "4    530301  730724       14  43.0°C  53.0V      3.4V     11.8V       主备\n",
       "5    530301  730151       14  36.0°C  53.1V      3.5V     11.9V       主备\n",
       "6    874001  588309       14  35.0°C  52.8V      3.5V     11.9V       主备\n",
       "7    530301  730089       14  40.0°C  53.0V      3.5V     11.9V       主备\n",
       "8    530301  730479       14  28.0°C  53.1V      3.5V     11.9V       主备\n",
       "9    530301  729985       14  37.0°C  53.7V      3.5V     11.9V       主备"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    '子网_SUBNET': subnet,\n",
    "    '基站代码_NE': ne,\n",
    "    '槽位号_SLOT': slot,\n",
    "    '模块温度': temprature,\n",
    "    '输入电源电压': voltage1,\n",
    "    '管理电源电压(V)': voltage2,\n",
    "    '业务电源电压(V)': voltage3,\n",
    "    'PM板的工作模式': mode,\n",
    "})\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-01-29T12:12:32.968904Z",
     "start_time": "2022-01-29T12:12:32.858904Z"
    }
   },
   "outputs": [],
   "source": [
    "with pd.ExcelWriter('结果输出.xlsx') as f:\n",
    "    df.to_excel(f, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
